package sisi.status;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sisi.General;


/**
 * StatusWindow.java - Controller class for the <i>Status window</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 17/03/2014
 * 
 */

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class StatusWindow extends Window implements 
				org.zkoss.zk.ui.ext.AfterCompose {
//window
private Window windowStatus, windowEditStatus;
private Textbox tbCerca;
private Listbox boxStatus;
private List lista_Status;
private boolean lNuovo;
private Toolbarbutton buttonSeleziona;
private ListModelList listModelList;
private Integer idSelezionato = 0;

protected Map<String, Object> args;
private Object StatusItem;
private Status modStatus;
// edit
private Textbox description;
private Textbox status;
private Intbox seq;

		public void onCreate$windowStatus(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N": tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		lista_Status = new StatusController().getList(null);
		listModelList.addAll(lista_Status);
		boxStatus.setModel(listModelList);
		boxStatus.setItemRenderer(new StatusItemRenderer());

		if (idSelezionato!=null && idSelezionato!=0){
		for (int i = 0; i < lista_Status.size(); i++) {
			Status record = (Status)lista_Status.get(i);
			if (record.getSeq()==idSelezionato.intValue()){
				boxStatus.setSelectedIndex(i);
				break;
			}
		}
	}

}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}

	public void onClick$buttonCerca(Event event) {
		cerca();
	}


	private void cerca(){
		listModelList = new ListModelList();
		lista_Status = new StatusController().getList(tbCerca.getValue());
		listModelList.addAll(lista_Status);
		boxStatus.setModel(listModelList);
		boxStatus.setItemRenderer(new StatusItemRenderer());
	}

	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}

	public void onClick$buttonSeleziona(Event event) {

		seleziona();

}

	public void seleziona() {
		if (boxStatus.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oStatus = boxStatus.getSelectedItem().getAttribute("rigaStatus");
			String codiceSelezionato = ""+((Status) oStatus).getSeq();
			Sessions.getCurrent().setAttribute("codiceStatusSelezionato", codiceSelezionato);
			windowStatus.detach();
		}
	}

	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}

	private void modifica(boolean nuovo){
		try {

			int nIndex = boxStatus.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a row to modify", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxStatus", boxStatus);
				Window finestraEditStatus = (Window) Executions.createComponents("/editStatus.zul", null, map);
				finestraEditStatus.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}

	public void onClick$buttonCancella(Event event) {
	// TODO Auto-generated method stub

	}

	public class StatusItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Status) data).getSeq()).setParent(li);
			new Listcell(""+((Status) data).getDescription()).setParent(li);
			li.setAttribute("rigaStatus", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}


// EDIT: NEW/MODIFY
	public void onCreate$windowEditStatus(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();

		boxStatus = (Listbox) args.get("boxStatus");

		if (args.containsKey("lNuovo")) {
			lNuovo = true;
		} else {
			lNuovo = false;
		}

		if (!lNuovo) {
			StatusItem = boxStatus.getSelectedItem().getAttribute("rigaStatus");
			modStatus = new StatusController().refresh((Status) StatusItem);
			StatusItem = modStatus;
			windowEditStatus.setTitle("Edit Status");
			description.setValue(modStatus.getDescription());
			status.setValue(modStatus.getStatus());
			seq.setValue(modStatus.getSeq());
		} else {
			windowEditStatus.setTitle("New Status");
		}

	}

	public void onClick$btnAnnulla(Event event){
		windowEditStatus.detach();
	}

	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modStatus = new Status();
		} else {

		}
		String testoMsg = "";

		modStatus.setDescription(description.getValue());
		modStatus.setStatus(status.getValue());
		modStatus.setSeq(seq.getValue());

		if (lNuovo){
			new StatusController().add(modStatus);
			testoMsg = "New Status: "+ General.trimNull( modStatus.getDescription() );
		} else {
			new StatusController().update(modStatus);
			testoMsg = "Update Status: "+ General.trimNull( modStatus.getDescription() );
		}

		General.showNotify( testoMsg, windowEditStatus);

		windowEditStatus.detach();

		// aggiorna listbox

		if (lNuovo){
			ListModelList lml = (ListModelList) boxStatus.getListModel();
			lml.add(modStatus);
			boxStatus.setSelectedIndex(lml.indexOf(modStatus));
		} else {
			ListModelList lml = (ListModelList) boxStatus.getListModel();
			Object oStatusItem = boxStatus.getSelectedItem().getAttribute("rigaStatus");
			if (lml.indexOf(oStatusItem) == -1) {
				// non trovato... quindi non lo aggiorno
			} else {
				lml.set(lml.indexOf(oStatusItem), modStatus);
			}
		}
	}

}
//

